package cn.com.dhc.danlu.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.com.dhc.danlu.R;

public class LinkPageActivity extends BaseActivity {
    /**
     * 图片资源
     */
    private static final int[] pics = {R.drawable.link_page_two, R.drawable.link_page_three, R.drawable.link_page_four};
    /**
     * 点击跳转
     */
    private Button linkBtn;
    /**
     * 滑动viewpager
     */
    private ViewPager linkViewPage;
    private List<View> views;
    private GestureDetector gestureDetector; // 用户滑动
    /**
     * 记录当前分页ID
     */
    private int flaggingWidth;// 互动翻页所需滚动的长度是当前屏幕宽度的1/3
    private int currentItem = 0; // 当前图片的索引号

    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;

        //  获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_link_page);

        linkBtn = (Button) findViewById(R.id.linkBtn);
        linkViewPage = (ViewPager) findViewById(R.id.linkViewPage);
        gestureDetector = new GestureDetector(new GuideViewTouch());
        // 获取分辨率
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        flaggingWidth = dm.widthPixels / 3;
        linkBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                gotoLoginActivity ();
            }
        });

        setViewPager();
    }

    @Override
    protected String getActivityTitle() {
        return null;
    }

    /**
     * 设置viewPager
     */
    private void setViewPager() {
        views = new ArrayList<>();

        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        // 初始化引导图片列表
        for (int pic : pics) {
            SimpleDraweeView sdv = new SimpleDraweeView(this);
            sdv.setLayoutParams(mParams);
            sdv.setImageResource(pic);
            //            sdv.setImageBitmap (readBitMap (delegate.getContext (), pic));
            sdv.setScaleType(ScaleType.CENTER_CROP);
            views.add(sdv);
        }
        // viewpAger适配器
        // 初始化Adapter
        MyAdapter adapter = new MyAdapter();
        linkViewPage.setAdapter(adapter);
        linkViewPage.setOnPageChangeListener(new MyPageChangeListener());
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)) {
            event.setAction(MotionEvent.ACTION_CANCEL);
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 进入主界面
     */
    public void gotoLoginActivity() {
        // 跳转到登录
        Intent intent = new Intent(this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            gotoLoginActivity();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class GuideViewTouch extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (currentItem == 3) {
                if (Math.abs(e1.getX() - e2.getX()) > Math.abs(e1.getY()
                        - e2.getY())
                        && (e1.getX() - e2.getX() <= (-flaggingWidth) || e1
                        .getX() - e2.getX() >= flaggingWidth)) {
                    if (e1.getX() - e2.getX() >= flaggingWidth) {
                        gotoLoginActivity();
                        return true;
                    }
                }
            }
            return false;
        }
    }

    /**
     * 当ViewPager中页面的状态发生改变时调用
     *
     * @author Administrator
     */
    private class MyPageChangeListener implements OnPageChangeListener {
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        public void onPageSelected(int position) {
            currentItem = position;
            if (position == 2) {
                linkBtn.setVisibility(View.VISIBLE);
            } else {
                linkBtn.setVisibility(View.GONE);
            }
        }

        public void onPageScrollStateChanged(int arg0) {
        }
    }

    /**
     * 填充ViewPager页面的适配器
     *
     * @author Administrator
     */
    private class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return pics.length;
        }

        @Override
        public void startUpdate(View arg0) {
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(views.get(arg1));
            return views.get(arg1);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }
    }
}